﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.Factories;

namespace TimeSplicerModule.Security
{
    public interface ISessionService
    {
        ISession Get(string login);
        ISession Get(string login, string password);
    }

    public class SessionService : ISessionService
    {
        private readonly IUserFactory _userFactory;
        private readonly IApplicationItemFactory _appItemFactory;
        private readonly IApplicationPermissionFactory _appPermissionFactory;
        private readonly IApplicationRoleFactory _appRoleFactory;
        private readonly IApplicationRolePermissionFactory _appRolePermissionFactory;
        private readonly IPriorityFactory _priorityFactory;
        private readonly IStatusFactory _statusFactory;


        public SessionService(IUserFactory userFactory, IApplicationItemFactory appItemFactory, IApplicationPermissionFactory appPermissionFactory, IApplicationRoleFactory appRoleFactory, 
            IApplicationRolePermissionFactory appRolePermissionFactory, IPriorityFactory priorityFactory, IStatusFactory statusFactory)
        {
            _userFactory = userFactory;
            _appItemFactory = appItemFactory;
            _appPermissionFactory = appPermissionFactory;
            _appRoleFactory = appRoleFactory;
            _appRolePermissionFactory = appRolePermissionFactory;
            _priorityFactory = priorityFactory;
            _statusFactory = statusFactory;
        }


        // TODO: Add Tasks and Prorities and any other items that stay static to the session object, that way can reduce some of the calls to the database

        public ISession Get(string login)
        {
            var session = new Session();
            session.User = _userFactory.Get(x => x.Login.ToLower() == login.ToLower());

            if (session.User != null)
            {
                // get permissions and everything
                var roles = _appRoleFactory.GetList();

                session.ApplicationItems = _appItemFactory.GetList();
                session.ApplicationPermissions = _appPermissionFactory.GetList();
                session.User.ApplicationRole = _appRoleFactory.Get(x => x.ApplicationRoleGuid == session.User.ApplicationRoleGuid);
                session.User.ApplicationRole.Permissions = _appRolePermissionFactory.GetList(x => x.ApplicationRoleGuid == session.User.ApplicationRole.ApplicationRoleGuid, roles, session.ApplicationItems, session.ApplicationPermissions);
                session.Priorities = _priorityFactory.GetList();
                session.Statuses = _statusFactory.GetList();
            }

            return session;
        }

        public ISession Get(string login, string password)
        {
            var session = new Session();
            session.User = _userFactory.Get(x => x.Login == login && x.Password == password);

            if (session.User != null)
            {
                // get permissions and everything
                var roles = _appRoleFactory.GetList();

                session.ApplicationItems = _appItemFactory.GetList();
                session.ApplicationPermissions = _appPermissionFactory.GetList();
                session.User.ApplicationRole = _appRoleFactory.Get(x => x.ApplicationRoleGuid == session.User.ApplicationRoleGuid);
                session.User.ApplicationRole.Permissions = _appRolePermissionFactory.GetList(x => x.ApplicationRoleGuid == session.User.ApplicationRole.ApplicationRoleGuid, roles, session.ApplicationItems, session.ApplicationPermissions);
                session.Priorities = _priorityFactory.GetList();
                session.Statuses = _statusFactory.GetList();
            }

            return session;
        }
    }
}
